package project;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import hashUtil.GeoHashUtil;
import tools.ByteOrderUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: sun
 * @Description:北京出租车矢量数据集合类
 * @Data: create in 15:58 2018/12/17
 * @Modfied by:
 */
public class TaxiVectorSet implements VectorSet {

    private long MaxCount = 0;

    @Override
    public void read(String url) throws IOException {
        File dataFile = new File(url);
        BufferedReader br = null;
        br = new BufferedReader(new FileReader(dataFile));
        String line = "";
        String carId = "";
        long count=0;
        BloomFilter<Long> bloomFilter = BloomFilter.create(Funnels.longFunnel(), 1000, 0.000001);
        List<String> list = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            //System.out.println("\tline: "+line);
            carId = line.split(",")[0];
            double lon = Double.parseDouble( line.split(",")[2]);
            double lat = Double.parseDouble( line.split(",")[3]);
            long geohash = GeoHashUtil.LongLatToHash(lon,lat);
            //System.out.println("\tGeohash : "+geohash);
            list.add(String.valueOf(geohash));
            count++;
            bloomFilter.put(geohash);
        }
        System.out.println("\tcarID: "+carId);
        System.out.println("\tcount:"+count);
        System.out.println("\tbloomFilter:"+bloomFilter.hashCode());
        System.out.println("\tbyte[]:"+ ByteOrderUtils.int2byte(bloomFilter.hashCode()));
        System.out.println("\tgeohashList:"+list.toString()+"\n");
        if(count>MaxCount){MaxCount=count;}

        /* 将bloomFilter输出到控制台
        bloomFilter.writeTo(System.out);
        */
    }

    @Override
    public void readDir(String dirUrl) throws IOException {
        File dir = new File(dirUrl);
        if(dir.exists()){
            File[] files = dir.listFiles();
            for(File file :files){
                System.out.println(file.getAbsolutePath());
                read(file.getAbsolutePath());
            }
        }
        System.out.println("MaxCount: "+MaxCount);
        BufferedReader bf = new BufferedReader(new FileReader(new File("H:\\我的坚果云\\毕业设计\\src\\main\\resources\\DATA\\testFile")));

    }

    /**
    public byte[] intToByte(int val){
        byte[] b = new byte[4];
        b[0] = (byte)(val & 0xff);
        b[1] = (byte)((val >> 8) & 0xff);
        b[2] = (byte)((val >> 16) & 0xff);
        b[3] = (byte)((val >> 24) & 0xff);
        return b;
    }
     */
}
